#!/usr/bin/env python
#--coding:utf-8--
import sys
import os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from 主机管理.models import SQLClass
import easygui
from 主机管理.models import loggingCls
loginst=loggingCls.logCls("../logs/users.log")
import hashlib
def mkpasswd(passwd):
    hash=hashlib.md5()
    hash.update(bytes(passwd,encoding="utf-8"))
    return hash.hexdigest()

#登录界面
def login():
    msg ="请输入以下信息"
    while True:

        ret=easygui.multpasswordbox(msg,"登录界面",fields=("username","password",))
        if ret:
            username, password= ret
        else:
            return

        a = SQLClass.SQLBase()
        a.addTable(SQLClass.Users)
        sel_ret=a.watch(filter=SQLClass.Users.username == username)
        if sel_ret and sel_ret[0].password==mkpasswd(password):
            easygui.msgbox("登录成功")
            loginst.infile("%slogging success"%username)
            return username
        else:
            msg="登录信息输入错误"
        a.commit()

#注册
def register():
    while True:
        ret=easygui.multpasswordbox("请输入以下信息","注册界面",fields=("username","password","再输入一遍"))
        if ret:
            username, password, again=ret
        else:
            return
        a = SQLClass.SQLBase()
        a.addTable(SQLClass.Users)
        sel_ret = a.watch(filter=SQLClass.Users.username == username)
        if sel_ret:
            easygui.msgbox("用户已经存在，请重新输入")
        elif password!=again:
            easygui.msgbox("两次密码输入不一致")
            loginst.infile("%s password error" % username)

        else:
            password=mkpasswd(password)
            a.add(username=username,password=password)
            a.commit()
            easygui.msgbox("恭喜成功注册")
            loginst.infile("%s register success" % username)
            break

#注销
def unregister():
    while True:
        ret=easygui.multpasswordbox("请输入以下信息","删除用户",fields=("username","password",))
        if ret:
            username, password = ret
        else:
            return
        a = SQLClass.SQLBase()
        a.addTable(SQLClass.Users)
        sel_ret = a.watch(filter=SQLClass.Users.username == username)
        if not sel_ret:
            easygui.msgbox("不存在此用户%s,请重新输入"%username)
        elif sel_ret[0].password!=mkpasswd(password):
            easygui.msgbox("密码验证错误")
        else:
            flag=easygui.boolbox("是否要删除用户%s"%username)
            if flag:
                a.delete(filter=SQLClass.Users.username == username)
                easygui.msgbox("成功删除用户%s"%username)
                loginst.infile("%s unregister success" % username)
                a.commit()
            break

#查看用户
def watchUser():
    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Users)
    user_list=[]
    for user in a.watch():
        user_list.append(user.username)
    easygui.choicebox("以下是所有用户",choices=user_list)

def changePassword():
    a = SQLClass.SQLBase()
    a.addTable(SQLClass.Users)
    username=easygui.enterbox("请输入用户")
    user_ret=a.watch(filter=SQLClass.Users.username==username)
    if not user_ret:
        easygui.msgbox("你输入的用户不存在，请重新输入")
    else:
        pass_list=["旧密码","新密码","重新输入新密码"]
        oldpassword,newpassword,again=easygui.multpasswordbox(fields=pass_list)
        if user_ret[0].password==mkpasswd(oldpassword):
            if newpassword==again:
                a.update(filter=SQLClass.Users.username==username,result={"password":mkpasswd(newpassword)})
    a.commit()
    easygui.msgbox("密码修改成功")
